<template>
 <div>
   <p class="f16">分类管理</p>
   <div class="mt-10">
     <Button @click="openModal" type="primary"><Icon type="md-add" />新增</Button>
   </div>
   <div class="mt-20 flex flex-column align-center justify-center">
    <Table :columns="columns" :data="tableDate" class="w-100"></Table>
    <Page 
      :total="page.total"
      :current="page.pageNum"
      :page-size="page.pageSize"
      show-sizer
      @on-change="pageChange"
      @on-page-size-change="pageSizeChange"
      class="my-20">
    </Page>
   </div>
   <Modal
      v-model="isShow"
      :title="content"
      @on-ok="onOk"
      @on-cancel="onCancel">
      <Form ref="sortList" :model="sortList" :rules="rulesSortList" :label-width="80">
        <FormItem label="新增分类" prop="name">
          <Input type="text" v-model="sortList.name" placeholder="请输入名称" />
        </FormItem>
        <FormItem label="排序值" prop="orderNum">
          <Input type="number" v-model="sortList.orderNum" placeholder="请输入排序值"/>
        </FormItem>
        <FormItem label="目录级别" prop="type">
          <Select v-model="sortList.type" filterable @on-change="typeChange">
            <Option v-for="item in 3" :key="item" :value="item">{{ item }}</Option>
          </Select>
        </FormItem>
        <FormItem label="上级目录ID:">
          <Input type="number" v-model="sortList.parentId" :disabled="true" />
        </FormItem>
      </Form>
   </Modal>
 </div>
</template>
<script>
import {adminCategoryList, categoryUpdate, categoryDelete, categoryAdd} from 'api'
export default {
  data () {
    return {
      isShow: false,
      sortList: {
        type: 0
      }, // 新增分类
      content: '新增分类',
      rulesSortList: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '请输入排序值', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择目录级别' }
        ]
      },
      columns: [
        {
          key: 'id',
          title: '分类ID',
          align: 'center'
        },
        {
          key: 'name',
          title: '分类名称',
          align: 'center'
        },
        {
          key: 'type',
          title: '目录级别',
          align: 'center'
        },
        {
          key: 'createTime',
          title: '添加时间',
          align: 'center',
          render: (h,params) => {
            return <div>
            { this.format(params.row.createTime, 'yyyy年MM月dd日hh时mm分ss秒') }
            </div>
          }
        },
        {
          title: '操作',
          align: 'center',
          render: (h,params) => {
            return <div>
              <Button onClick={this.redact.bind(this, params.row)} class="mr-10">
              <Icon type="md-create" />编辑</Button>
              <Button type="error" onClick={this.delete.bind(this, params.row)}>
              <Icon type="md-trash" />删除</Button>
            </div>
          }
        },
      ],
      tableDate: [],
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 10,
      }
    }
  },
  created() {
    this.adminCategoryList()
  },
  mounted() {

  },
  
  methods: {
    // list
    adminCategoryList() {
      adminCategoryList(this.page).then(res => {
        this.tableDate = res.list
        this.page.pageNum = res.pageNum
        this.page.pageSize = res.pageSize
        this.page.total = res.total
      })
    },
    // 父id自动确认
    typeChange() {
      if (this.sortList.type <= 0) {
        this.sortList.parentId = ''
      } else {
        this.sortList.parentId = this.sortList.type -1
      }
    },
    // 新增
    openModal() {
      this.sortList = {}
      this.isShow = true
      this.content = '新增分类'
    },
    // 新增编辑
    onOk() {
      if (this.sortList['id']) {
        categoryUpdate(this.sortList).then(res => {
          this.$Message.success('修改成功！')
        })
      } else {
        categoryAdd(this.sortList).then(res => {
          this.$Message.success('上传成功！')
        })
      }
      this.adminCategoryList()
    },
    // 取消
    onCancel() {
      this.sortList = {}
    },
    // 编辑
    redact(data) {
      this.isShow = true
      this.content = '编辑分类'
      this.sortList = data
    },
    // 删除
    delete(data) {
      this.$Modal.confirm({
        content: `你确定要删除${data.name}分类吗？`,
        onOk: () => {
          categoryDelete(data.id).then(res =>{
            this.$Message.success('删除成功！')
            this.adminCategoryList()
          })
        }
      })
    },
    // 页码
    pageChange(pageNum) {
      this.page.pageNum = pageNum
      this.adminCategoryList()
    },
    // 页数
    pageSizeChange(pageSize) {
      this.page.pageSize = pageSize
      this.adminCategoryList()
    },
    format(date, fmt) {
      date = new Date(date)
      const o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'h+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        'S': date.getMilliseconds() // 毫秒
      }
      if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) }
      for (const k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
        }
      }
      return fmt
    }

  },
}
</script>
<style lang='less' scoped>
</style>